;======================================================
; This script is used to read and wrtie ***.nc.
;====================================================== 
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/wrf/WRFUserARW.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/wrf/WRF_contributed.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/contrib/ut_string.ncl"
load "/public/users/wrf/Codes/WRF_scripts/utils/substring.ncl"

begin

; enable to write large NetCDF file
setfileoption("nc","Format","LargeFile")

; 
var_list = (/ "SST", "Times", "HGT", "ZNU", "ZNW", "U", "V", "U10", "V10", "T", "T2", "SSWDN", "RAINC", "RAINNC", "P", "PSFC", "PB", "QVAPOR", "Q2", "PH", "PHB", "XLAT", "XLONG"/)

nVar = dimsizes(var_list)

; test if input_file is given
if (.not. isvar("input_file")) then
    input_file = "/public/users/wrf/wrfout/2012112820/wrfout_d02_2012-11-28_12:00:00"
    print("input_file is undefined, using default = " + input_file)
else
    print("Using input_file: : " + input_file)
end if

output_file = input_file + ".strip"

a = addfile(input_file + ".nc","r")

if (isfilepresent(output_file)) then
    system("rm " + output_file + ".nc")
end if
fo = addfile(output_file + ".nc","c")
filedimdef(fo, "Time", -1, True)
;----------------------------------- 
; read old field and write new field
;-----------------------------------
do i = 0, nVar-1, 1
    var = a->$var_list(i)$
    fo->$var_list(i)$ = var
    delete(var)
end do

fileattdef(fo,a)

end
